yjx.controller('orderDetailController',['$rootScope','$scope','$stateParams','$fetch','$timeout','modalService','$init','ipCookie','$state',function($rootScope,$scope,$stateParams,$fetch,$timeout,modalService,$init,ipCookie,$state){
$init.title("订单详情");
var orderId = $stateParams.id;
var api = '/unified/order/detail';
$scope.paying = false;
function getDetail(){
    $fetch.fetch(
        api,
        {
            params:{
                orderId:orderId
            }
            // cache:false
        }
    ).then(function(data){
        $timeout(function(){
            $scope.data = data;
            if($rootScope.from == 'orderPay'){
                $rootScope.changeState = true;
                $rootScope.changeStateName = 'index';
                $rootScope.from = null;
            }
            if(data.coupons){
                // getCoupons();
            }
        })     
    })
}
if(orderId){
    getDetail();
}


 /**微信支付 */
 $scope.getPrepay = function(data) {
    $scope.paying = true;
    var params = {
        orderId: data,
        // openid: $rootScope.user.openid,
    };
    //  alert(params.orderId+"===="+params.openid);
    $fetch.fetch(
        '/stayOrder/callPay',
        {
            params: params,
            type: 'get',
            cache: false,
        }
    ).then(function (data) {
        $timeout(function () {
            chooseWXPay(data);
        })
    }).catch(function (e) {
        $scope.paying = false;
        if (e) {
            modalService.mainMsgError(e.msg)
        } else {
            modalService.mainMsgError("服务器出错")
        }
    })
}
function chooseWXPay(data) {
    function onBridgeReady() {
        WeixinJSBridge.invoke(
            'getBrandWCPayRequest', {
                "appId": data.appId,     //公众号名称，由商户传入
                "timeStamp": data.timeStamp,         //时间戳，自1970年以来的秒数
                "nonceStr": data.nonceStr, //随机串
                "package": data.package,
                "signType": data.signType,         //微信签名方式：
                "paySign": data.paySign //微信签名
            },
            function (res) {
                //alert(res.err_msg)
                if (res.err_msg == "get_brand_wcpay_request:ok") {
                    //alert(res.err_msg)
                    $timeout(function () {
                        $scope.paying = false;
                        modalService.mainMsgInfo("支付成功")
                        getDetail();
                    })
                }     // 使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回    ok，但并不保证它绝对可靠。
                else {
                    modalService.mainMsgInfo("支付失败")
                    $scope.paying = false;
                }
            }
        );
    }
    if (typeof WeixinJSBridge == "undefined") {
        if (document.addEventListener) {
            document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
        } else if (document.attachEvent) {
            document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
            document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
        }
        modalService.mainMsgInfo("请先关注基因团公众号<br>并在微信浏览器在中完成支付")
    } else {
        onBridgeReady();
    }
}
$rootScope.user =  $rootScope.user? $rootScope.user:ipCookie('user');
/**测试数据 */
// $scope.data = {
//     orderNo:'',
//     orderNum:2,
//     name:'王福生',
//     telephone:13456789890,
//     toolAddress:'华南生汨罗市新塘乡',
//     reportAddress:'湖南省汨罗市新塘乡',
//     productIds:'1,2',
//     discountValue:200,
//     totalPrice:999
// }

// $scope.productList = [
//     {
//         id: 1,
//         avatar: './common/img/home/icon_commission1.png',
//         name: '常道康-健康无忧全肠道菌群检测1',
//         describe: '针对您的全基因进行完整的测序',
//         price: 2333,
//         originalPrice: 9999,
//         salesVolume: 99,
//         captainBrokerage: 88
//     },
//     {
//         id: 2,
//         avatar: './common/img/home/icon_commission1.png',
//         name: '常道康-健康无忧全肠道菌群检测2',
//         describe: '针对您的全基因进行完整的测序',
//         price: 2333,
//         originalPrice: 9999,
//         salesVolume: 99,
//         captainBrokerage: 88
//     },]
}])